from PyQt5.QtWidgets import QMainWindow, QMenu, QAction, QVBoxLayout, QWidget, QTabWidget, QHBoxLayout

from uuid import UUID

from scan import scan
from scrape import scrape
from repair import repair_main_db as repair

from player.dialogs.sync import SyncDialog
from player.widgets.lists.track import TrackList
from player.widgets.lists.artist import ArtistList
from player.widgets.lists.album import AlbumList
from player.widgets.lists.genre import GenreList
from player.widgets.lists.cover import CoverList
from player.widgets.editors.track import TrackEditor

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Music Repository")

        #region
        menuBar = self.menuBar()
        #region
        self.fileMenu = QMenu("&File", self)
        menuBar.addMenu(self.fileMenu)
        # self.fileMenu.addSeparator()
        #region
        quitAction = QAction("&Quit", self)
        quitAction.setShortcut("Ctrl+Q")
        quitAction.setStatusTip('Leave the app')
        quitAction.triggered.connect(self.close)
        self.fileMenu.addAction(quitAction)
        #endregion
        #endregion
        #region
        self.databaseMenu = QMenu("&Database", self)
        menuBar.addMenu(self.databaseMenu)
        #region
        scanAction = QAction("&Scan", self)
        scanAction.setStatusTip('Scan for new files - WARNING: App may freeze while this is running')
        scanAction.triggered.connect(scan)
        self.databaseMenu.addAction(scanAction)
        #endregion
        #region
        scrapeAction = QAction("&Scrape", self)
        scrapeAction.setStatusTip('Scrape existing files for missing metadata - WARNING: App may freeze while this is running')
        scrapeAction.triggered.connect(scrape)
        self.databaseMenu.addAction(scrapeAction)
        #endregion
        #region
        repairAction = QAction("&Repair", self)
        repairAction.setStatusTip('Repair the main database - WARNING: App may freeze while this is running')
        repairAction.triggered.connect(repair)
        self.databaseMenu.addAction(repairAction)
        #endregion
        self.fileMenu.addSeparator()
        #region
        syncAction = QAction("&Sync", self)
        syncAction.setStatusTip('Sync library to a directory')
        syncAction.triggered.connect(self.sync_lib)
        self.databaseMenu.addAction(syncAction)
        #endregion
        #endregion
        #endregion

        #region
        layout = QVBoxLayout()
        widget = QWidget()
        widget.setLayout(layout)

        self.track_list = TrackList()
        self.artist_list = ArtistList()
        self.album_list = AlbumList()
        self.genre_list = GenreList()
        self.cover_list = CoverList()

        self.track_editor = TrackEditor()
        self.track_editor.setDisabled(True)
        self.track_list.track_list.clicked.connect(lambda: self.track_editor.load(UUID(self.track_list.track_list_model.itemFromIndex(self.track_list.track_list_model.index(self.track_list.track_list.selectedIndexes()[0].row(), self.track_list.TRACK_UUID)).text())))
        self.track_editor.track_updated.connect(self.track_list.refresh_track_list)

        track_tab_layout = QHBoxLayout()
        self.track_tab = QWidget()
        self.track_tab.setLayout(track_tab_layout)
        track_tab_layout.addWidget(self.track_list)
        track_tab_layout.addWidget(self.track_editor)

        self.tab_widget = QTabWidget()
        self.tab_widget.addTab(self.track_tab, 'Tracks')
        self.tab_widget.addTab(self.artist_list, 'Artists')
        self.tab_widget.addTab(self.album_list, 'Albums')
        self.tab_widget.addTab(self.genre_list, 'Genres')
        self.tab_widget.addTab(self.cover_list, 'Covers')
        layout.addWidget(self.tab_widget)

        self.setCentralWidget(widget)
        #endregion

    def sync_lib(self):
        dialog = SyncDialog(self)
        dialog.exec_()
